package com.sgb.kjwl.view.event;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.sgb.kjwl.view.ui.adapter.shopping.NormalRecyclerAdapter;
import com.sgb.kjwl.view.ui.adapter.shopping.NormalRecyclerAdapter1;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewBannerNormal extends RecyclerViewBannerBase<LinearLayoutManager, NormalRecyclerAdapter1> {

    public RecyclerViewBannerNormal(Context context) {
        this(context, null);
    }

    public RecyclerViewBannerNormal(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecyclerViewBannerNormal(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        float x1;
        if (ev.getX()<0)
        {
            x1=ev.getX()-50;
        }
        else
        {
            x1=ev.getX()+50;
        }
        System.out.println("x1=="+x1);
        long l = SystemClock.uptimeMillis();
        System.out.println("时间=="+ l);
        //时间==278008634
        System.out.println("y=="+ev.getY());
        MotionEvent moveEvent = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x1, ev.getY(), ev.getMetaState());
        return super.onInterceptTouchEvent(moveEvent);
    }

    @Override
    protected void onBannerScrolled(RecyclerView recyclerView, int dx, int dy) {
        //解决连续滑动时指示器不更新的问题
        if (bannerSize < 2) return;
        int firstReal = mLayoutManager.findFirstVisibleItemPosition();
        View viewFirst = mLayoutManager.findViewByPosition(firstReal);
        float width = getWidth();
        if (width != 0 && viewFirst != null) {
            float right = viewFirst.getRight();
            float ratio = right / width;
            if (ratio > 0.8) {
                if (currentIndex != firstReal) {
                    currentIndex = firstReal;
                        refreshIndicator();
                }
            } else if (ratio < 0.2) {
                if (currentIndex != firstReal + 1) {
                    currentIndex = firstReal + 1;
                        refreshIndicator();
                }
            }
        }
    }

    @Override
    protected void onBannerScrollStateChanged(RecyclerView recyclerView, int newState) {
        int first = mLayoutManager.findFirstVisibleItemPosition();
        int last = mLayoutManager.findLastVisibleItemPosition();
        if (currentIndex != first && first == last) {
            currentIndex = first;
            refreshIndicator();
        }
    }

    @Override
    protected LinearLayoutManager getLayoutManager(Context context, int orientation) {
        return new LinearLayoutManager(context, orientation, false);
    }

    @Override
    protected NormalRecyclerAdapter1 getAdapter(Context context, List<String> list, boolean mIsMore,boolean b,int type) {


        NormalRecyclerAdapter1 normalRecyclerAdapter = new NormalRecyclerAdapter1(context,list,b,mIsMore,type);
      /*  // 点击回调
        normalRecyclerAdapter.setOnItemClickListener((adapter, view, position) -> onBannerItemClickListener.onItemClick(position));
        // 长按回调
        normalRecyclerAdapter.setOnItemLongClickListener((adapter, view, position) -> {
            onBannerItemLongClickListener.onItemClick(position);
            return true;
        });*/

        return normalRecyclerAdapter;
    }
}